package edu.guigu.networkdemo.nonsingle;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import edu.guigu.networkdemo.network.GithubClient;
import edu.guigu.networkdemo.databinding.ActivityNonSingleViewBinding;
import edu.guigu.networkdemo.model.User;

public class NonSingleViewActivity extends AppCompatActivity {

    private ActivityNonSingleViewBinding binding;
    private NonSingleViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

        binding = ActivityNonSingleViewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(binding.main, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();
        updateData2();
    }

    private void initView(){
        adapter = new NonSingleViewAdapter(getApplication());
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(NonSingleViewActivity.this));
        binding.recyclerView.setAdapter(adapter);
    }
    private void updateData2() {
        GithubClient githubClient = GithubClient.getInstance();
        githubClient.listUsers().enqueue(new retrofit2.Callback<List<User>>() {
            @Override
            public void onResponse(retrofit2.Call<List<User>> call, retrofit2.Response<List<User>> response) {
                List<User> userList = response.body();
                // 到UI线程更新视图
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(NonSingleViewActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
                        adapter.setUserList(userList);
                        adapter.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onFailure(retrofit2.Call<List<User>> call, Throwable t) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(NonSingleViewActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

//    private void updateData(){
//
//        Call call = UserClient.getInstance().getUserList("https://api.github.com/users");
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NonNull Call call, @NonNull IOException e) {
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Toast.makeText(NonSingleViewActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
//                    }
//                });
//            }
//
//            @Override
//            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//                // 拿到 响应数据 json字符串
//                String jsonString = response.body().string();
//                Log.d("TAG", "onResponse: " + jsonString);
//                // 解析json字符串， 反序列化（json字符串 -> List<实体>）
//                Gson gson = new Gson();
//                Type listType = new TypeToken<List<User>>() {}.getType();
//                List<User> userList = gson.fromJson(jsonString, listType);
//
//                // 到UI线程更新视图
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Toast.makeText(NonSingleViewActivity.this, "请求成功", Toast.LENGTH_SHORT).show();
//                        adapter.setUserList(userList);
//                        adapter.notifyDataSetChanged();
//                    }
//                });
//            }
//        });
//    }
}